package es.ibm.santillana.mdm.xml;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Element;

import com.ibm.pim.attribute.AttributeInstance;
import com.ibm.pim.catalog.item.Item;
import com.ibm.pim.context.Context;

import es.ibm.santillana.mdm.common.util.ConstSantillana;
import es.ibm.santillana.mdm.integration.Modification;

public class UpdateXML extends XMLIntegration{

	public UpdateXML(Context ctx, String catalogName, String sPk) throws ParserConfigurationException {
		super(ctx,sPk, catalogName);
	}

	@Override
	public String getExportXML(HashMap<String,ArrayList<Modification>> hmInsertAtts) {
		Element elementMensajeRoot = getUpdateXML();
		
		Element head = createHead();
		
		Element body = null;
		if(getCatalogName().equals(ConstSantillana.MAESTRO_ARTICULOS)){
			//REcorrer elementos del body:
			body = createBodyUpdate(hmInsertAtts,"update");
		}else{
			body = createTableBody(hmInsertAtts.get(ConstSantillana.XMLTags.Body.tablasReferencia),getCatalogName(), getsPk(),"update");
		}
		
	 
		
		elementMensajeRoot.appendChild(head);
		elementMensajeRoot.appendChild(body);
		
		
		return getStringFromDoc(getDoc());
	}
	
	public Element getUpdateXML(){
		return createRootElement("update");
	}

	
	public Element createTableElementfromModif(Modification modification){
		Element genericElement = super.createTableElementfromModif(modification);
//		if(genericElement.hasChildNodes()){
//			Element elementpk = null; 
//			
//			elementpk = createpkTableElement();
//			
//			genericElement.appendChild(elementpk);
//		}
		return genericElement;
	}
	

	
	private Element createpkTableElement() {
		Element pkElement = getDoc().createElement("pk");
		Element codElement =  getDoc().createElement("COD_" + getCatalogName());
		codElement.appendChild(getDoc().createTextNode(getsPk()));
		pkElement.appendChild(codElement);
		return pkElement;
	}
	
	
}
